#!/usr/bin/env python
# -*- coding: utf-8 -*-

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.api import urlfetch

from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
from xml.dom import minidom
import logging
import time
import datetime
import os
import oauth
import simplejson
import urllib
import urllib2
import random
import sys
import urllib
from init import *
"""
    XML Resource
        - Kizasi : http://kizasi.jp/rss.xml
        - Yahoo : rank5
        - 
    Twitter
    RSS Feed
"""
class ScrapeData(db.Model):
    pass
class MixiWordDB(db.Model):
    type=db.StringProperty()
    create_day=db.DateTimeProperty()
    day=db.StringProperty()
    data=db.TextProperty()

class TodayWordDB(db.Model):
    day=db.StringProperty()
    create_day=db.DateTimeProperty()
    word=db.TextProperty()
class RssToJson():
    def __init__(self, url):
        self.url = url
        self.Get()
        self.Parse()
    def Get(self):
        self.fetch = urlfetch.fetch(self.url)
    def Parse(self):
        dom = {}
        _dom = minidom.parseString(self.fetch.content)
        dom["title"] = _dom.getElementsByTagName('title')[0].firstChild.data.encode("utf-8")
        dom["item"] = []
        _item = _dom.getElementsByTagName('item')
        for items in _item:
            nodes = {}
            for ch in items.childNodes:
                nodeName = ch.nodeName.encode("utf-8")
                if len(items.getElementsByTagName(nodeName)) is 1:
                    firstChild = items.getElementsByTagName(nodeName)[0].firstChild
                    if firstChild != None:
                        text = firstChild.data.encode("utf-8")
                    nodes[nodeName] = text
            dom["item"].append(nodes)
        self.dom = dom
    def ToJson(self):
        import simplejson
        return simplejson.dumps(self.dom)
       
class YahooJLP():
    def __init__(self,appid=None):
        self.url = 'http://jlp.yahooapis.jp/MAService/V1/parse'
        self.appid = appid
        #print appid
    def Get(self, sentence=None, result=None):
        if not result:
            result = 'ma'
        url = "%s?appid=%s&sentence=%s&results=%s" % (self.url, self.appid, urllib.quote(sentence.encode("UTF-8")), result)
        self.urls = url;
        fetch = urlfetch.fetch(url)
        #print fetch.status_code
        return fetch
        
    def ParseTo(self, str):
        dom = {}
        _dom = minidom.parseString(str)
        dom['total_count'] = _dom.getElementsByTagName('total_count')[0].firstChild.data.encode("utf-8")
        dom["filtered_count"] = _dom.getElementsByTagName('filtered_count')[0].firstChild.data.encode("utf-8")
        dom["word"] = []# Dictionary
        _list = _dom.getElementsByTagName('word')
        for items in _list:
            nodes = {}
            for ch in items.childNodes:
                # text=""
                nodeName = ch.nodeName.encode("utf-8")
                firstChild = items.getElementsByTagName(nodeName)[0].firstChild
                if firstChild != None:
                    text = firstChild.data.encode("utf-8")
                nodes[nodeName] = text
            dom["word"].append(nodes)
        return dom
    def ToJson(self, dect):
        import simplejson
        return simplejson.dumps(dect)
    def GetJson(self, word=None):
        if word is not None:
            w = self.Get(word)
            dic = self.ParseTo(w.content)
            return self.ToJson(dic)
        else:
            return self.ToJson({'status':'error', 'error_code':'500'})
        
class KizasiWordXML(webapp.RequestHandler):
    def __init__(self):
        self.xml = 'http://kizasi.jp/rss.xml'
    def get(self):
        get = self.request.get('word');
        #RSS = RssToJson('http://searchranking.yahoo.co.jp/rss/word5-rss.xml')
        RSS = RssToJson(self.xml)
        s = simplejson.loads(RSS.ToJson())
        arr = []
        YDK = YahooJLP('WjIojuyxg65IN8lzY5yvacimEULhZHI__oSaLThkKoEcuu13jRogqTxF1vzJ.upC0X8-')
        for i,it in enumerate(s["item"]):
            date = it["pubDate"]
            title = it["title"].encode("UTF-8");
            str = YDK.GetJson(word=it["title"])
            s = simplejson.loads(str)
            s.update({"pubDate":date, "title":title})
            arr.append(s)
        # self.response.out.write(len(arr))
        # self.response.headers['Content-Type'] = 'text/javascript; charset=utf-8;'
        json=YDK.ToJson(arr)
        #t = datetime.timedelta(hours=9)
        dates=GetGMT(hour=9)
        db=MixiWordDB(type="json",create_day=dates,data=json,day=dates.strftime("{'year':%Y,'month':%m,'day':%d}"))
        db.put()
        #self.response.out.write(d)

class YahooWordXML(webapp.RequestHandler):
    def __init__(self):
        self.xml = 'http://searchranking.yahoo.co.jp/rss/word5-rss.xml'
    def get(self):
        get = self.request.get('word');
        RSS = RssToJson(self.xml)
        s = simplejson.loads(RSS.ToJson())
        arr = []
        YDK = YahooJLP('WjIojuyxg65IN8lzY5yvacimEULhZHI__oSaLThkKoEcuu13jRogqTxF1vzJ.upC0X8-')
        for i,it in enumerate(s["item"]):
            date = it["pubDate"]
            title = it["title"].encode("UTF-8");
            str = YDK.GetJson(word=it["title"])
            s = simplejson.loads(str)
            s.update({"pubDate":date, "title":title})
            arr.append(s)
        #self.response.out.write(len(arr))
        #self.response.headers['Content-Type'] = 'text/javascript; charset=utf-8;'
        json=YDK.ToJson(arr)
        dates=GetGMT(hour=9)
        db=MixiWordDB(type="json",create_day=dates,data=json,day=dates.strftime("{'year':%Y,'month':%m,'day':%d}"))
        # db=MixiWordDB(type="json",create_day=dates,data=json,day=dates.strftime("{'year':2011,'month':11,'day':01}"))
        db.put()
        #self.response.out.write(datetime.datetime.today())
        
class TodayWord(webapp.RequestHandler):
    def __init__(self):
        # 一日一回 : 0:30自動更新
        self.gql = ''
    def get(self):
        q=MixiWordDB.all()
        dates=GetGMT(hour=9)
        day=dates.strftime("{'year':%Y,'month':%m,'day':%d}")
        #day=dates.strftime("{'year':%Y,'month':%m,'day':02}")
        #q = db.GqlQuery("SELECT * FROM MixiWordDB " + "WHERE day = :1", day)
        #q.filter("day =", day)
        q.order("-create_day")
        q.fetch(10)
        """
            自動収集したワードを日付で再取得し、選別/自動処理
        """
        d=[]
        for i in q:
            j=simplejson.loads(i.data)
            #self.response.out.write("%s, %s, %s<br>" % (i.create_day,i.day,j[1]["word"][0]["reading"]))
            for k in j:
                r = k["word"][0]["reading"]
                #self.response.out.write("%s, %s, %s<br>" % (i.create_day,i.day,j[1]["word"][0]["reading"]))
                #self.response.out.write("%s<br>" % r[len(r)-1])
                """
                    追加分岐処理：一文字と最後の文字が「ん」もしくはアルファベットで始まらないor終わらない
                    
                """
                d.append({
                          "start":r[0],
                          "last":r[len(r)-1],
                          "reading":k["word"][0]["reading"],
                          "pos":k["word"][0]["pos"],
                          "surface":k["word"][0]["surface"]
                })
                
        if len(d) == 0 :
            return False
        self.response.out.write(d[0]["reading"])
        randint=random.randint(0, (len(d)-1))
        json=simplejson.dumps(d[randint])
        """
            今日のワードを保存/cron自動処理/毎日3:00頃？
        """
        #self.response.out.write(dates)
        """
            Today DataStore
        """
        count=TodayWordDB.all().count()
        if count > 0:
            #self.response.out.write(count)
            db=TodayWordDB.all()
            day=dates.strftime("{'year':%Y,'month':%m,'day':%d}")
            #day=dates.strftime("{'year':%Y,'month':%m,'day':02}")
            db.filter("day =", day)
            res=db.fetch(db.count())
            jsonstr = json
            if len(res) > 0:
                json=simplejson.loads(res[0].word)
                self.response.out.write(json["reading"])
            else:
                CreateWord=TodayWordDB(create_day=dates,day=day,word=jsonstr)
                CreateWord.put()
                # pass
        else:
            CreateWord=TodayWordDB(create_day=dates,day=day,word=json)
            CreateWord.put()
            #TodayWordDB.all()
            #self.response.out.write("今日のきーわーどなし")
            

application = webapp.WSGIApplication([('/word/mxyh', YahooWordXML),
                                      ('/word/mxkz', KizasiWordXML),
                                      ('/word',TodayWord)],
                                      debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
